/*
 * PreferencesDialog.cpp
 *
 *  Created on: Jan 1, 2009
 *      Author: sez
 */

#include <QSettings>
#include <QFileDialog>
#include <QDebug>

#include <DndData/DndData.h>
#include <Xml/XmlException.h>
#include <Xml/XmlDirectory.h>

#include "PreferencesDialog.h"

PreferencesDialog::PreferencesDialog(QWidget *parent) :
    QDialog(parent), xmlDir(0)
{
    mPrefs.setupUi(this);
    QString message;
    try
    {
        xmlDir = new XmlDirectory();
        mPrefs.dataFilePathLineEdit->setText(xmlDir->dataRootDir().absolutePath());
        updateDataPathWarnings();
    }
    catch (XmlException e)
    {
        updateDataPathWarnings(e.message());
    }
}

PreferencesDialog::~PreferencesDialog()
{
    delete xmlDir;
}


/*!
  \brief Return the instance of the PreferencesDialog dialog.
  This method is first called during the construction of the QMainWindow,
  and at this time the instance is initialized with the main window as its
  \a parent.  With this invariant, the method is thread-safe, otherwise it
  is not.  On subsequent calls the already initialized instance is returned
  and any argument is ignored.
 */
PreferencesDialog *PreferencesDialog::dialog(QWidget *parent)
{
    static PreferencesDialog *dlg = 0;
    if (dlg == 0)
        dlg = new PreferencesDialog(parent);
    return dlg;
}

void PreferencesDialog::on_dataFilePathPushButton_clicked(bool)
{
    QString message = tr("Select directory to store DndTool data files");
    QString initialDir = mPrefs.dataFilePathLineEdit->text();
    qDebug() << "initialDir" << initialDir;
    if (xmlDir && xmlDir->isValid())
        initialDir = xmlDir->dataRootDir().absolutePath();
    qDebug() << "from xmldir" << initialDir;
    QString dir = QFileDialog::getExistingDirectory(this, message,
            initialDir, QFileDialog::ShowDirsOnly
                    | QFileDialog::DontResolveSymlinks);
    mPrefs.dataFilePathLineEdit->setText(dir);
    updateDataPathWarnings();
}

void PreferencesDialog::on_dataFilePathLineEdit_textEdited(const QString &text)
{
    QDir rootDir(text);
    QString message;
    if (!rootDir.exists())
        message = text + tr(" directory does not exist");
    updateDataPathWarnings(message);
}

void PreferencesDialog::updateDataPathWarnings(const QString &text)
{
    bool pathValid = text.isEmpty();
    mPrefs.dataFolderWarningLabel->setText(text);
    mPrefs.dataFolderWarningLabel->setVisible(!pathValid);
    mPrefs.dataFolderWarningIcon->setVisible(!pathValid);
    mPrefs.buttonBox->setEnabled(pathValid);
    mPrefs.buttonBox->setToolTip(pathValid ? tr("Save this path to settings") :
        tr("Type or choose a valid path to enable saving"));
}

void PreferencesDialog::on_buttonBox_accepted()
{
    QString dirPath = mPrefs.dataFilePathLineEdit->text();
    xmlDir->setDataRootDir(QDir(dirPath));
    emit statusMessage(tr("%1 set as new data directory").arg(dirPath));
    emit dataFilePathUpdated(dirPath);
    qDebug() << "buttonBox accepted" << dirPath;
    done(QDialog::Accepted);
}
